﻿using Furion.DynamicApiController;
using FurionApiDemo.Core;
using FurionApiDemo.Core.ApiResult;
using FurionApiDemo.Core.Implement.html;
using FurionApiDemo.Core.Implement.html.Dtos;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FurionApiDemo.Application.Services
{
    /// <summary>
    /// 
    /// </summary>
    [Route("html")]
    public class HtmlAppService : IDynamicApiController
    {
        private readonly IHtmlService _htmlService;

        private readonly ILogger<HtmlAppService> _logger;

        public HtmlAppService(IHtmlService htmlService, ILogger<HtmlAppService> logger)
        {
            _htmlService = htmlService;
            _logger = logger;
        }
        /// <summary>
        /// 获取目录
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetDirectoryList")]
        public async Task<ApiResult<List<FileItem>>> GetDirectoryList()
        {
            var result = new ApiResult<List<FileItem>>();
            try
            {
                result.Data = await _htmlService.GetDirectoryList();

            }
            catch (Exception ex)
            {
                result.Code = ResultCode.ERRO;
                result.Message = ex.Message;
                _logger.LogError(ex.Message);
            }
            return result;
        }
        /// <summary>
        /// 获取文件详情
        /// </summary>
        /// <param name="uuid"></param>
        /// <returns></returns>
        [HttpGet("GetItemFileInfo")]
        public async Task<ApiResult<object>> GetItemFileInfo(string uuid)
        {
            var result = new ApiResult<object>();
            try
            {
                result.Data = await _htmlService.GetItemFileInfo(uuid);

            }
            catch (Exception ex)
            {
                result.Code = ResultCode.ERRO;
                result.Message = ex.Message;
                _logger.LogError(ex.Message);
            }
            return result;
        }
        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="title"></param>
        /// <param name="coverImage"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost("SaveItemFile")]
        public async Task<ApiResult<object>> SaveItemFile(SaveItemFileInputDto input)
        {
            var result = new ApiResult<object>();
            try
            {
                result.Data = await _htmlService.SaveItemFile(input);

            }
            catch (Exception ex)
            {
                result.Code = ResultCode.ERRO;
                result.Message = ex.Message;
                _logger.LogError(ex.Message);
            }
            return result;
        }
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="uuid"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost("DeleteItemFile")]
        public async Task<ApiResult<bool>> DeleteItemFile(string uuid)
        {
            var result = new ApiResult<bool>();
            try
            {
                result.Data = await _htmlService.DeleteItemFile(uuid);

            }
            catch (Exception ex)
            {
                result.Code = ResultCode.ERRO;
                result.Message = ex.Message;
                _logger.LogError(ex.Message);
            }
            return result;
        }
        /// <summary>
        /// 恢复删除的文件
        /// </summary>
        /// <param name="uuid"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost("RestoreItemFile")]
        public async Task<ApiResult<bool>> RestoreItemFile(string uuid)
        {
            var result = new ApiResult<bool>();
            try
            {
                result.Data = await _htmlService.RestoreItemFile(uuid);

            }
            catch (Exception ex)
            {
                result.Code = ResultCode.ERRO;
                result.Message = ex.Message;
                _logger.LogError(ex.Message);
            }
            return result;
        }
        /// <summary>
        /// 保存item
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost("SaveItem")]
        public async Task<ApiResult<bool>> SaveItem(SaveItemInputDto input)
        {
            var result = new ApiResult<bool>();
            try
            {
                result.Data = await _htmlService.SaveItem(input);

            }
            catch (Exception ex)
            {
                result.Code = ResultCode.ERRO;
                result.Message = ex.Message;
                _logger.LogError(ex.Message);
            }
            return result;
        }
        /// <summary>
        /// 删除item
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost("DeleteItem")]
        public async Task<ApiResult<bool>> DeleteItem(DeletetemInputDto input)
        {
            var result = new ApiResult<bool>();
            try
            {
                result.Data = await _htmlService.DeleteItem(input);

            }
            catch (Exception ex)
            {
                result.Code = ResultCode.ERRO;
                result.Message = ex.Message;
                _logger.LogError(ex.Message);
            }
            return result;
        }
        /// <summary>
        /// 更新所有item
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost("UpdateAllItem")]
        public async Task<ApiResult<bool>> UpdateAllItem(UpdateAllItemInputDto input)
        {
            var result = new ApiResult<bool>();
            try
            {
                result.Data = await _htmlService.UpdateAllItem(input);

            }
            catch (Exception ex)
            {
                result.Code = ResultCode.ERRO;
                result.Message = ex.Message;
                _logger.LogError(ex.Message);
            }
            return result;
        }

    }
}
